package com.hoshiibuy.company.controller;


import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.hoshiibuy.common.rpc.Response;
import com.hoshiibuy.company.service.IComDepartmentsService;
import com.hoshiibuy.company.entity.ComDepartments;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * <p>
 * 部门 前端控制器
 * </p>
 *
 * @author WGJ
 * @since 2019-04-22
 */
@RestController
@RequestMapping("/department")
@Api(value = "部门管理", tags = {"部门操作接口"})
public class ComDepartmentsController {

    @Autowired
    private IComDepartmentsService departmentsService;

    /**
     * @param id
     * @return com.hoshiibuy.common.rpc.Response
     * @description: 根据id查询部门信息
     */
    @GetMapping("/{id}")
    @ApiOperation(
            value = "查询",
            notes = "查询部门信息",
            produces = "application/json, application/xml",
            consumes = "application/json, application/xml")
    public Response findDeptById(Long id) {

        return Response.data(departmentsService.getById(id));
    }

    /**
     * @param
     * @return com.hoshiibuy.common.rpc.Response
     * @description: 根据名称查询
     */
    @GetMapping("/find/{name}")
    @ApiOperation(value = "查询",
            notes = "根据名称查询",
            produces = "application/json, application/xml",
            consumes = "application/json, application/xml")
    public Response findDeptByName(String name) {

        Wrapper wrapper = Wrappers.<ComDepartments>query().lambda().eq(ComDepartments::getName, name);

        return Response.data(departmentsService.getOne(wrapper));

    }

    /**
     * @param departments
     * @return com.hoshiibuy.common.rpc.Response
     * @description: 新增
     */
    @PostMapping("/save")
    @ApiOperation(value = "新增",
            notes = "新增部门信息",
            produces = "application/json, application/xml",
            consumes = "application/json, application/xml")
    @Cacheable(cacheNames = "departments", key = "#root.targetClass.simpleName+':'+#root.methodName+':'+#id")
    public Response saveDept(@Valid ComDepartments departments) {
        /**
         * TODO 查询公司
         */

        return Response.data(departmentsService.save(departments));
    }

    /**
     * @param departments
     * @return com.hoshiibuy.common.rpc.Response
     * @description: 修改
     */
    @PutMapping("/update")
    @ApiOperation(value = "修改",
            notes = "修改部门信息",
            produces = "application/json, application/xml",
            consumes = "application/json, application/xml")

    public Response updateDeptById(@Valid ComDepartments departments) {
        return Response.data(departmentsService.updateById(departments));

    }
}
